<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '#' }">
                运动会系统信息管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>
                <a href="javascript:void(0)">数据库管理</a>
            </el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="ar-card">
            <div class="card_fun">
                <div class="card_a">
                    <el-button type="primary" @click="showcopy">备份</el-button>
                    <el-button type="primary" @click="restoreDatabase">恢复</el-button>
                    <el-button type="primary" @click="sqlDelete">删除</el-button>
                </div>
                <el-button type="primary" @click="clearAlldata">重建比赛信息</el-button>
            </div>

            <el-table :data="aechiveData" border ref="myTable" highlight-current-row @row-click="handleRowClick">
                <el-table-column type="index" label="编号" width="60">
                </el-table-column>
                <el-table-column prop="backupsname" label="备份文件名" align="center">
                    <template #default="scope">
                        {{ formatBackupsname(scope.row.backupsname) }}
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            aechiveData: [],
            backups: {},
            backval: null,
        }
    },
    created() {
        this.postArchiveList();
    },
    methods: {
        // 查询
        async postArchiveList() { // 数据
            try {
                const result = await this.$http.post(this.BACKEND_URL + '/backup/getmsg');
                if (result.data.code == 0) {
                    this.aechiveData = result.data.data
                } else if (result.data.code == 500) {
                    console.log(result.data.msg)
                }
            } catch (error) {
                if (error === undefined) {
                    this.$message.error('连接错误-获取数据失败');
                }
            }
        },
        formatBackupsname(filename) {
            return filename.replace('.txt', '.sql');
        },
        handleRowClick(row) {
            this.backups = row
            this.backval = this.aechiveData.indexOf(row)
        },
        // 备份
        async showcopy() {
            const result = await this.$http.post(this.BACKEND_URL + '/backup/beifen');
            if (result.data.code == 0) {
                this.postArchiveList()
                this.$message({
                    type: 'success',
                    message: '备份成功!'
                });
            } else if (result.data.code == 500) {
                console.log(result.data.msg)
            }
        },
        // 恢复
        restoreDatabase() {
            if (this.backval !== null) {
                this.$confirm('恢复该数据库吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: 'true'
                }).then(async () => {
                    const result = await this.$http.get(this.BACKEND_URL + '/backup/recover', {
                        params: { id: this.backups.id },
                    })
                    if (result.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: '数据库导入成功！'
                        });
                        this.postArchiveList()
                    } else if (result.data.code == 500) {
                        this.$message.error(result.data.msg)
                    }
                }).catch(() => {
                });
            } else {
                this.$message.error('请选择需恢复的数据！');
            }
        },
        // 删除
        sqlDelete() {
            if (this.backval !== null) {
                this.$confirm('此操作将删除该文件，是否继续', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(async () => {
                    const result = await this.$http.get(this.BACKEND_URL + '/backup/delete', {
                        params: { id: this.backups.id },
                    })
                    if (result.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.postArchiveList()
                    } else if (result.data.code == 500) {
                        this.$message.error(result.data.msg)
                    }
                }).catch(() => {
                });
            } else {
                this.$message.error('请选择需删除的数据！');
            }
        },
        // 清空
        // clearAll() {
        //     // 获取全部数据
        //     this.$confirm('该操作将删除全部数据，是否执行', '提示', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning',
        //         center: 'true'
        //     }).then(async () => {
        //         await this.$http.post(this.BACKEND_URL + '/backup/deleteAll')
        //         this.postArchiveList()
        //     }).catch(() => {
        //     });
        // },
        // 清空参赛人员
        clearAlldata() {
            this.$confirm('该操作将删除全部比赛数据，包括单位和参赛人员信息、比赛成绩和积分等，是否删除', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(async () => {
                const result = await this.$http.post(this.BACKEND_URL + '/backup/deleteMsg')
                if (result.data.code === 0) {
                    this.postArchiveList()
                    this.$message({
                        type: 'success',
                        message: '重建比赛信息成功!'
                    });
                } else if (result.data.code == 500) {
                    this.$message.error(result.data.msg)
                }
            }).catch(() => {
            });
        }
    }
}
</script>

<style lang="less" scoped>
.ar-card {
    margin-top: 15px;

    .el-table {
        margin-top: 15px;
    }
}

.card_fun {
    display: flex;

    .card_a {
        text-align: left;
        flex: 1;
    }
}
</style>